package com.hxkj.service.analyze.impl;

import com.hxkj.client.service.ColumnArticleServiceClient;
import com.hxkj.domain.enums.ColumnExceptionEnum;
import com.hxkj.domain.enums.TagsExceptionEnum;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.Column;
import com.hxkj.domain.po.ColumnArticleTree;
import com.hxkj.domain.po.Tags;
import com.hxkj.service.analyze.ColumnArticleService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @ClassName ColumnArticle
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/7/2 15:00
 * @Version 1.0
 */
@Service
@Slf4j
public class ColumnArticleServiceImpl implements ColumnArticleService{
    private static final Logger LOGGER = LoggerFactory.getLogger(ColumnArticleServiceImpl.class);

    @Autowired
    private ColumnArticleServiceClient columnArticleServiceClient;

    @Override
    public ColumnArticleTree getColumnArticle(Column column, PageTo pageParam) throws Exception{
        ColumnArticleTree columnArticleTree = new ColumnArticleTree();
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        column.setDelTag((byte) 0);
        ParameterDataStruct<Column> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(column);
        parameterDataStruct.setPageTo(pageParam);
        if (column.getSiteId() == null){
            throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_SITEID);
        }
        if (column.getColumnId() == null){
            throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_EXIST);
        }
        try {
            columnArticleTree = columnArticleServiceClient.getColumnArticle(parameterDataStruct);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取分类文章列表失败{}",1);
        }
        return columnArticleTree;
    }

    @Override
    public ResultPageStruct<Article> getTagsArticle(Tags tags, PageTo pageParam) throws Exception{
        if (tags.getSiteId()==null){
            throw new CustomException(TagsExceptionEnum.TAGS_NOT_SITEID);
        }
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<Tags> parameterDataStruct =new ParameterDataStruct<>();
        parameterDataStruct.setPageTo(pageParam);
        parameterDataStruct.setData(tags);
        ResultPageStruct<Article> resultPageStruct = new ResultPageStruct<>();
        try {
            resultPageStruct = columnArticleServiceClient.getTagsArticle(parameterDataStruct);
            resultPageStruct.setMessage(null);
            resultPageStruct.setCode(null);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("获取标签列表失败");
        }
        return resultPageStruct;
    }
}
